package com.example.cmh.controller;

import com.example.cmh.common.PageResult;
import com.example.cmh.common.Result;
import com.example.cmh.pojo.Course;
import com.example.cmh.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @PostMapping("/add")
    public Result<String> addCourse(@RequestBody Course course, @RequestParam(name = "currentUserId") Integer currentUserId) {
        return courseService.addCourse(course, currentUserId);
    }

    @PutMapping("/update/{isAdmin}")
    public Result<String> updateCourse(@PathVariable Boolean isAdmin,@RequestBody Course course) {
        return courseService.updateCourse(isAdmin,course);
    }

    @DeleteMapping("/delete")
    public Result<String> deleteCourse(@RequestParam(name = "courseId") Integer courseId) {
        return courseService.deleteCourse(courseId);
    }

    @GetMapping("/search")
    public Result<List<Course>> searchCourse(
            @RequestParam(name = "courseName", required = false) String courseName,
            @RequestParam(name = "courseSort", required = false) Integer courseSort,
            @RequestParam(name = "currentUserId") Integer currentUserId) {
        return courseService.searchCourse(courseName, courseSort, currentUserId);
    }

    @GetMapping("/detail")
    public Result<Course> getCourseDetail(@RequestParam(name = "courseId") Integer courseId) {
        return courseService.getCourseDetail(courseId);
    }
    
    @GetMapping("/list-page")
    public Result<PageResult<Course>> getCourseListWithPage(
            @RequestParam(name = "courseName", required = false) String courseName,
            @RequestParam(name = "courseSort", required = false) Integer courseSort,
            @RequestParam(name = "currentUserId") Integer currentUserId,
            @RequestParam(name = "current", defaultValue = "1") Integer current) {
        return courseService.getCourseListWithPage(courseName, courseSort, currentUserId, current);
    }
    
    @PostMapping("/review")
    public Result<String> reviewCourse(@RequestParam(name = "courseId") Integer courseId, @RequestParam(name = "status") Integer status) {
        return courseService.reviewCourse(courseId, status);
    }
} 